$(function() {
	// 渲染页面-通用部分；
	commTemplUtil.initPage();
	// 渲染左侧菜单;
	commTemplUtil.initCommLeftMemu();

	var addrTemplObj = _.template($('#addr-option-templ').html());
	var categTemplObj = _.template($('#category-templ').html());

	// 地址下拉框选项;
	renderPage(addrTemplObj);

	// 大类/小类, 下拉框选项;
	renderAddcategory(addrTemplObj, categTemplObj);

	$("#myform1").validate({
		rules: {
			name: "required",
			category_1: "required",
			category_2: "required",
			price: {
				required: true,
				number: true,
			},
			number: {
				required: true,
				digits: true,
			},
			moq: {
				required: true,
				number: true,
			},
			adress: {
				required: true,
				number: true,
			},
			company: "required",
			offer_1: {
				required: true,
				rangelength: [2, 16],
			}
		},
		messages: {
			offer_1: "2-16个字符，不允许填写电话和特殊符号",
			category_1: "请选择一个大的类别",
			category_2: "请选择一个小的的类别",
			adress: "请选择一个地区",
			number: "请输入一个整数",
		},
	});

});

function renderPage(addrTemplObj) {

	// 省
	$.ajax({
		url: PAGE_CONFIG.requestUrl.addrProvince,
		type: 'get',
		dataType: 'json',
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = addrTemplObj(datas);
			$("#province").html(tmplObjHtml);

			$("#province").on("change", function() {
				var code = $(this).val();
				renderAddrCity(code, addrTemplObj);
			});
			// typeof PAGE_DATA != 'undefined'
			if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {

				setDefaulAddr("province", PAGE_DATA.addrDefault.province_id || "");
			}
		},
		error: function(xhr, status, error) {
			var tmplObjHtml = addrTemplObj([]);
			$("#province").html(tmplObjHtml);
		}
	});


};

function renderAddrCity(provinceCode, addrTemplObj) {
	// 市
	$.ajax({
		url: PAGE_CONFIG.requestUrl.addrCity,
		type: 'post',
		dataType: 'json',
		data: {
			code: provinceCode,
			_token: COMM_DATA.csrfToken
		},
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = addrTemplObj(datas);
			$("#city").html(tmplObjHtml);
			$("#city").on("change", function() {
				var code = $(this).val();
				renderAddrCounty(code, addrTemplObj);

			});
			if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
				setDefaulAddr("city", PAGE_DATA.addrDefault.city_id || "");
			};
		},
		error: function(xhr, status, error) {
			var tmplObjHtml = addrTemplObj([]);
			$("#city").html(tmplObjHtml);
		}
	});

	$("#city").empty().html(addrTemplObj([]));
	$("#county").empty().html(addrTemplObj([]));
};

function renderAddrCounty(cityCode, addrTemplObj) {

	// 区/县
	$.ajax({
		url: PAGE_CONFIG.requestUrl.addrCounty,
		type: 'post',
		dataType: 'json',
		data: {
			code: cityCode,
			_token: COMM_DATA.csrfToken
		},
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = addrTemplObj(datas);
			$("#county").html(tmplObjHtml);

			if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
				setDefaulAddr("county", PAGE_DATA.addrDefault.county_id || "");
			}
		},
		error: function(xhr, status, error) {
			var tmplObjHtml = addrTemplObj([]);
			$("#county").html(tmplObjHtml);
		}
	});
};

function renderAddcategory(addrTemplObj, categTemplObj) {

	$.ajax({
		url: PAGE_CONFIG.requestUrl.offerInfo1,
		type: 'post',
		dataType: 'json',
		data: {
			_token: COMM_DATA.csrfToken
		},
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = categTemplObj(datas);
			$("#category_1").html(tmplObjHtml);
			$("#category_1").on("change", function() {
				var categorys = $(this).val();
				if (categorys == "") {

				} else {
					$("#category_2").empty().html(addrTemplObj([]));
					renderAddcategory_2(categorys, categTemplObj);
				};
			});

			if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
				setDefaulAddr("category_1", PAGE_DATA.addrDefault.category_1 || "");
			}

		},
		error: function(xhr, status, error) {}
	});

};

function renderAddcategory_2(category, categTemplObj) {

	//小类
	$.ajax({
		url: PAGE_CONFIG.requestUrl.offerInfo2,
		type: "post",
		dataType: "json",
		data: {
			_token: COMM_DATA.csrfToken
		},
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = categTemplObj(datas);
			$("#category_2").html(tmplObjHtml);
			 if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
			 	setDefaulAddr("category_2", PAGE_DATA.addrDefault.category_2 || "");
			 }
		}
	});
};

function setDefaulAddr(domId, value) {

	$("#" + domId).val(value).trigger("change");

};